package com.market.web.model;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.primefaces.event.SelectEvent;
import org.primefaces.model.SelectableDataModel;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.market.domain.Order;
import com.market.domain.Order.STATUS;
import com.market.domain.OrderDetail;
import com.market.domain.Product;
import com.market.domain.User;
import com.market.domain.transients.Email;
import com.market.domain.transients.OrderQuery;

@Component("orderModel")
@Scope("session")
public class OrderModel extends GenericModel<Order> {

	//Use this property when edit order detail information.
	private OrderDetail currentOrderDetail;
	//Use this property when send email to all customer in List. Don't save this email.
	private Email email = new Email();
	private OrderQuery orderQuery = new OrderQuery();
	
	/**
	 * Set whether show order detail in prepare mode,
	 * Use this mode to do order preparation.
	 */
	private Boolean isPrepareMode = Boolean.FALSE;

	public Boolean getIsPrepareMode() {
		return isPrepareMode;
	}

	public void setIsPrepareMode(Boolean isPrepareMode) {
		this.isPrepareMode = isPrepareMode;
	}
	
	/**
	 * Set whether show order detail in complete mode. Under this mode, ordered, confirmed, prepared/delivered
	 * information will be shown.
	 * 
	 * If order's status is received, ordered and confirmed information will be show.
	 * If order's status is confirmed, confirmed and prepared/delivered information will be shown.
	 */
	private Boolean isCompleteMode = Boolean.FALSE;
	
	
	public Boolean getIsCompleteMode() {
		return isCompleteMode;
	}

	public void setIsCompleteMode(Boolean isCompleteMode) {
		this.isCompleteMode = isCompleteMode;
	}

	/**
	 * Set whether show list panel in administration;
	 */
	private Boolean isShowListPanel=Boolean.TRUE;
	
	
	public Boolean getIsShowListPanel() {
		return isShowListPanel;
	}

	public void setIsShowListPanel(Boolean isShowListPanel) {
		this.isShowListPanel = isShowListPanel;
	}

	
	/**
	 * Set whether show detail panel in administration;
	 */
	private Boolean isShowDetailPanel=Boolean.TRUE;	

	public Boolean getIsShowDetailPanel() {
		return isShowDetailPanel;
	}

	public void setIsShowDetailPanel(Boolean isShowDetailPanel) {
		this.isShowDetailPanel = isShowDetailPanel;
	}

	public List<SelectItem> getAvailableStatus() {
		List<SelectItem> statuses = new ArrayList<SelectItem>();
		for (Order.STATUS status : Order.STATUS.values()) {
			statuses.add(new SelectItem(status, status.getLabel()));
		}
		return statuses;
	}

	public void onRowSelectUser(SelectEvent event) {
		this.getCurrent().setCustomer((User) event.getObject());

	}

	@Override
	public Order getNewObject() {
		Order order = new Order();
		order.setIsNew(true);
		this.setCurrent(order);
		return order;
	}

	public OrderDetail createOrderDetail(Product product) {
		OrderDetail orderDetail = new OrderDetail();
		orderDetail.setIsNew(true);
		orderDetail.setProduct(product);
		orderDetail.setOrderedPrice(product.getSpecialPrice());
		orderDetail.setOrderedQuantity(1);
		orderDetail.setSpecification(product.getSpecification());
		return orderDetail;
	}

	public OrderDetail getCurrentOrderDetail() {
		return currentOrderDetail;
	}

	public void setCurrentOrderDetail(OrderDetail currentOrderDetail) {
		this.currentOrderDetail = currentOrderDetail;
	}

	public Email getEmail() {
		return email;
	}

	public void setEmail(Email email) {
		this.email = email;
	}

	public OrderQuery getOrderQuery() {
		return orderQuery;
	}

	public void setOrderQuery(OrderQuery orderQuery) {
		this.orderQuery = orderQuery;
	}

	public Boolean getIsCurrentStatusReceived() {
		if (this.getCurrent().getStatus() == STATUS.RECEIVED) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	public Boolean getIsCurrentStatusConfirmed() {
		if (this.getCurrent().getStatus() == STATUS.CONFIRMED) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	public Boolean getIsCurrentStatusPrepared() {
		if (this.getCurrent().getStatus() == STATUS.PREPARED) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	public Boolean getIsCurrentStatusDelivered() {
		if (this.getCurrent().getStatus() == STATUS.DELIVERED) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	public Boolean getIsCurrentStatusPickedup() {
		if (this.getCurrent().getStatus() == STATUS.PICKED_UP) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	public Boolean getIsCurrentStatusCanceled() {
		if (this.getCurrent().getStatus() == STATUS.CANCELED) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	/**
	 * 
	 * @return Can current Order be paid?
	 */
	
	public Boolean getIsCurrentStatusCanBePaid() {
		if (this.getCurrent().getStatus() != STATUS.UNKNOWN
				&& this.getCurrent().getStatus() != STATUS.RECEIVED
				&& this.getCurrent().getStatus() != STATUS.CANCELED) {

			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

}
